#include "Key.h"
#include "gpio.h"
#include "usart.h"



void Key_Init(MyKeyTypeDef* MyKey)
{
    if (MyKey == NULL)
    {
        // 添加错误处理或调试信息
        DMA_Printf("Key Init Error!\r\n");
        return;
    }

    MyKey->KEY_Time = 0;
    MyKey->KEY_count = 0;
    MyKey->KEY_Press_Flag = 0;
    MyKey->KEY_LONG_Press = 0;
    MyKey->KEY_Count_ENABLE =false;

    // 添加调试信息
    // DMA_Printf("Key Init success, KEY_count: %d\r\n", MyKey->KEY_count);
}

void Key_Scan(MyKeyTypeDef* MyKey)
{
    // HAL_GPIO_WritePin(LOGIC_GPIO_Port,LOGIC_Pin,GPIO_PIN_RESET);
    if (MyKey == NULL)
    {
        // 添加错误处理或调试信息
        DMA_Printf("Scan Error: MyKey is NULL!\r\n");
        return;
    }

    if(KEY1_Read() == 0 && MyKey->KEY_Press_Flag == 0)//按键按下
    {
        MyKey->KEY_count++;
        MyKey->KEY_Press_Flag = 1;
        MyKey->KEY_Count_ENABLE = true;
    }
    else if(KEY1_Read() == 1 && MyKey->KEY_Press_Flag == 1)
    {
        MyKey->KEY_Press_Flag = 0;
    }
    if(MyKey->KEY_Press_Flag) MyKey->KEY_LONG_Press++; 
    // HAL_GPIO_WritePin(LOGIC_GPIO_Port,LOGIC_Pin,GPIO_PIN_SET);

}

void Key_State_Judge(MyKeyTypeDef* MyKey)
{
    if(MyKey->KEY_count == 1 && MyKey->KEY_LONG_Press < 5) MyKey->KEY_State = 1;//单击
    else if(MyKey->KEY_count == 2) MyKey->KEY_State = 2;//双击
    else if(MyKey->KEY_LONG_Press >= 5) MyKey->KEY_State = 3;//长按
    
}

